package com.aaa.outbound.controller;




import com.aaa.entity.outbound.Outorder;
import com.aaa.entity.outbound.Pick;
import com.aaa.entity.outbound.Picktask;
import com.aaa.entity.system.DictType;
import com.aaa.entity.system.User;
import com.aaa.outbound.feign.SystemFeign;
import com.aaa.outbound.service.OutorderService;
import com.aaa.outbound.service.PickService;
import com.aaa.outbound.service.PicktaskOutorderService;
import com.aaa.outbound.service.PicktaskService;
import com.aaa.util.JwtUtils;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjf
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/outbound/pick")
@Slf4j
public class PickController {
    @Autowired
    private PickService pickService;
    @Autowired
    private OutorderService outorderService;
    @Autowired
    private PicktaskService picktaskService;
    @Autowired
    private PicktaskOutorderService picktaskOutorderService;
    @Autowired
    private SystemFeign systemFeign;

    @PostMapping("searchPick/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询")
    public R searchPick(@ApiParam(value = "当前页数") @PathVariable Integer page,
                        @ApiParam(value = "每页个数") @PathVariable Integer limit,
                        @ApiParam(value = "查询条件") @RequestBody Pick pick){
        Page<Pick> pickPage=pickService.searchPick(page,limit,pick);
        return R.ok().message("分页查询成功").code(200).data("pick",pickPage);
    }
    //拣货任务单号
    @PostMapping("searchPicktaskno")
    @ApiOperation(value = "查询所有拣货任务单号")
    public R searchPicktaskno(){
        List<Picktask> list = picktaskService.list();
        List list1=new ArrayList();
        for (int i=0;i<list.size();i++){
            String picktaskno = list.get(i).getPicktaskno();
            list1.add(picktaskno);
        }
        return R.ok().data("picktaskno",list1).code(200);
    }

    //拣货
    @PostMapping("addPick/{picktaskno}/{remarks}")
    @ApiOperation(value = "拣货")
    public R addPick(@ApiParam(value = "拣货任务单号")@PathVariable String picktaskno,
                     @ApiParam(value = "备注")@PathVariable String remarks,
                     HttpServletRequest request){
        List<Picktask> picktask = picktaskService.list();
        QueryWrapper<Pick> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("picktaskno",picktaskno);
        Pick pick1 = pickService.getOne(queryWrapper);
        if (pick1==null) {
            for (int i = 0; i < picktask.size(); i++) {
                if (picktask.get(i).getPicktaskno().equals(picktaskno)) {

                    String token = request.getHeader("token");
                    Long userid = JwtUtils.getMemberIdByJwtToken(token);
                    User user = systemFeign.selectUserById(userid);

                    Pick pick = new Pick();
                    Long picktaskid = picktask.get(i).getId();
                    pick.setPicktaskid(picktaskid);
                    pick.setPicktaskno(picktaskno);
                    pick.setRemarks(remarks);
                    pick.setState("0");
                    pick.setCreator(user.getName());
                    Date now = new Date();
                    pick.setCreatetime(now);
                    pickService.addPick(pick);
                    Integer outorderId = picktaskOutorderService.findOutOrderIdByPicktaskId(picktaskid);
                    Outorder outorder = outorderService.getById(outorderId);
                    outorder.setState("4");
                    outorderService.updateById(outorder);
                    QueryWrapper<Picktask> queryWrapper1=new QueryWrapper<>();
                    queryWrapper1.eq("picktaskno",picktaskno);

                    Picktask picktask1 = picktaskService.getOne(queryWrapper1);

                    picktask1.setReceiver(user.getName());
                    picktask1.setReceivetime(now);
                    picktaskService.updateById(picktask1);

                    return R.ok().message("拣货成功").code(200);
                }
            }
            return R.error().message("该单号不存在");
        }
        return R.error().message("该单号已被拣货");
    }
}

